<template>

    <button @click="changeNode">修改节点颜色事件</button>

    <h2>1、节点动画</h2>
    <h2>2、通过属性赋值，添加节点</h2>

    <div id="chart-diagram" style="background-color: whitesmoke; width: 1000px; height: 1000px;">
    </div>


</template>

<script setup>
    import {onMounted} from 'vue';
    import go from "gojs";

    let myDiagram = null;


    const changeNode = () => {
        var data = myDiagram.model.findNodeDataForKey("Delta");
        // 这个方式可以更新
        if (data !== null) myDiagram.model.setDataProperty(data, "color", "red");
    }

    const init = () => {

        const $ = go.GraphObject.make

        myDiagram = new go.Diagram("chart-diagram", {
            'grid.visible': true, // 显示网格
            'clickCreatingTool.archetypeNodeData': {category: 'Normal', text: '新节点', notice: ''}, // 双击新建节点(可以写入节点的默认信息)；；
            'toolManager.hoverDelay': 100, // tooltip提示显示延时
            'toolManager.toolTipDuration': 10000, // tooltip持续显示时间
            allowMove: true, // 允许拖动画板
            allowDragOut: true, // 允许拖拽节点
            allowDelete: true, // 允许删除节点
            allowCopy: true, // 允许复制节点
            allowClipboard: true, // 允许粘贴节点

        });

        myDiagram.nodeTemplate =
            $(go.Node, "Auto",
                {locationSpot: go.Spot.Center,
                selectionAdorned: false//不显示选中框
                },
                $(go.Shape, "RoundedRectangle",
                    { // 自定义highlight
                        fill: "yellow", stroke: "orange", strokeWidth: 2
                    },
                    new go.Binding("fill", "highlight", function (v) {
                        return v ? "pink" : "lightblue";
                    }),
                    new go.Binding("stroke", "highlight", function (v) {
                        return v ? "red" : "blue";
                    }),
                    new go.Binding("strokeWidth", "highlight", function (v) {
                        return v ? 3 : 1;
                    })),
                $(go.TextBlock,
                    {margin: 5},
                    new go.Binding("text", "key"))
            );


        myDiagram.model.nodeDataArray = [
            {key: "Alpha", highlight: false},  // just one node, and no links
            {key: "Beta", highlight: false},  // just one node, and no links
            {key: "good", highlight: false},  // just one node, and no links
            {key: "hello", highlight: false}
        ];


        function flash() {
            // 所有模型更改都应该发生在事务中
            myDiagram.model.commit(function (m) {
                //var data = m.nodeDataArray[0];  // 获取第一个数组node的值
                var data = m.findNodeDataForKey("Alpha"); //跟上面这个，一样，获取到节点数据

                m.set(data, "highlight", !data.highlight);//设置data值里的highlight为!data.highlight
            }, "flash");
        }

        function loop() {
//每0.5秒执行一次修改
            setTimeout(function () {
                flash();
                loop();
            }, 500);
        }

        loop();


    };

    onMounted(() => {
        init();
    });


</script>

<style scoped>

</style>